﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Snake_DELALANDE_PELLEGRINI
{
    class Appat
    {
        private int appatWidth, appatHeight, appatTimeoutDisparition, appatTimeoutApparition;
        private bool isVisible;
        private Texture2D appatBITTexture, appatBITDEBUGTexture;
        private Vector2 originBIT, BITpos;
        private Rectangle BITbounds;
        private static Random posXBIT, posYBIT, seed;
        private enum behavior : int
        {
            Appat = 1,
            Special = 2,
            Faster = 3,
            Slower = 4,
            Grow = 5,
            Shrink = 6
        };
        private behavior myAppat;
        private Params settings;

        //CONSTRUCTEUR
        public Appat()
        {
            settings = new Params();
            seed = new Random();
            posXBIT = new Random(seed.Next(10, 1000));
            posYBIT = new Random(seed.Next(10, 1000));
            appatTimeoutDisparition = new Random(seed.Next(100, 200)).Next(500, 750);
            appatTimeoutApparition = new Random(seed.Next(100, 200)).Next(500, 750);

            appatBITTexture = null;
            appatBITDEBUGTexture = null;
            appatWidth = 25;
            appatHeight = 25;

            myAppat = behavior.Appat;
            isVisible = true;

            BITbounds = Rectangle.Empty;
            BITpos = Vector2.Zero;
            originBIT.X = appatWidth / 2;
            originBIT.Y = appatHeight / 2;
        }

        //PARAMETRAGE DE LA NOUVELLE POSITION D'UN APPAT
        public void setNewPositionBIT()
        {
            BITpos = new Vector2(posXBIT.Next(100, settings.screenWidth - 100), posYBIT.Next(100, settings.screenHeight - 100));
            BITbounds = new Rectangle((int)BITpos.X, (int)BITpos.Y, 25, 25);
        }

        public void lowerTimer()
        {
            if (myAppat == behavior.Special || myAppat == behavior.Faster || myAppat == behavior.Slower || myAppat == behavior.Grow || myAppat == behavior.Shrink)
            {
                if (appatTimeoutDisparition <= 0)
                {
                    isVisible = false;
                    //appatTimeoutDisparition = new Random(seed.Next(10, 1000)).Next(200, 500);
                    //appatTimeoutApparition = new Random(seed.Next(10, 1000)).Next(40, 70);
                    switch (myAppat)
                    {
                        case behavior.Special:
                            appatTimeoutDisparition = new Random(seed.Next(10, 1000)).Next(1000, 1250);
                            appatTimeoutApparition = new Random(seed.Next(10, 1000)).Next(750, 900);
                            break;
                        case behavior.Faster:
                        case behavior.Slower:
                            appatTimeoutDisparition = new Random(seed.Next(10, 1000)).Next(1500, 1750);
                            appatTimeoutApparition = new Random(seed.Next(10, 1000)).Next(600, 800);
                            break;
                        case behavior.Grow:
                        case behavior.Shrink:
                            appatTimeoutDisparition = new Random(seed.Next(10, 1000)).Next(1750, 2000);
                            appatTimeoutApparition = new Random(seed.Next(10, 1000)).Next(500, 750);
                            break;
                    }
                    Console.WriteLine(appatTimeoutApparition);
                    Console.WriteLine(appatTimeoutDisparition);
                }
                appatTimeoutDisparition--;
            }

            if (!isVisible)
            {
                if (appatTimeoutApparition <= 0)
                {
                    isVisible = true;
                }
                appatTimeoutApparition--;
            }
        }

        public void changeBehavior()
        {
            switch (myAppat)
            {
                case behavior.Appat:
                    isVisible = true;
                    break;
                case behavior.Special:
                case behavior.Faster:
                case behavior.Slower:
                case behavior.Grow:
                case behavior.Shrink:
                    isVisible = false;
                    break;
            }
        }

        public string getBehavior()
        {
            switch (myAppat)
            {
                case behavior.Appat:
                default:
                    return "Appat";
                case behavior.Special:
                    return "Special";
                case behavior.Faster:
                    return "Boost";
                case behavior.Slower:
                    return "Slow";
                case behavior.Grow:
                    return "Grow";
                case behavior.Shrink:
                    return "Shrink";
            }
        }

        //RENVOIE DE LA POSITION D'UN APPAT
        public Vector2 getPositionBIT()
        {
            return BITpos;
        }

        //RENVOIE DE LA HTIBOX D'UN APPAT
        public Rectangle getBoundsBIT()
        {
            return BITbounds;
        }

        public bool getIsVisible()
        {
            return isVisible;
        }

        public Texture2D getAppatTexture()
        {
            return appatBITTexture;
        }

        public Texture2D getAppatTextureDEBUG()
        {
            return appatBITDEBUGTexture;
        }

        public void setIsVisible(bool visibilite)
        {
            appatTimeoutDisparition = new Random(seed.Next(0, (2 ^ 20))).Next(500, 1000);
            appatTimeoutApparition = new Random(seed.Next(0, (2 ^ 20))).Next(1500, 2500);
            isVisible = visibilite;
        }

        public void setAppatTexture(Texture2D laTexture)
        {
            appatBITTexture = laTexture;
        }

        public void setAppatTextureDEBUG(Texture2D laTexture)
        {
            appatBITDEBUGTexture = laTexture;
        }

        public void setBehavior(int type)
        {
            switch (type)
            {
                case 1:
                    myAppat = behavior.Appat;
                    break;
                case 2:
                    myAppat = behavior.Special;
                    break;
                case 3:
                    myAppat = behavior.Faster;
                    break;
                case 4:
                    myAppat = behavior.Slower;
                    break;
                case 5:
                    myAppat = behavior.Grow;
                    break;
                case 6:
                    myAppat = behavior.Shrink;
                    break;

            }
            changeBehavior();
        }

    }
}
